//
//  HomeTableViewController.m
//  LOL
//
//  Created by lanou3g on 15/11/16.
//  Copyright © 2015年 syx. All rights reserved.
//

#import "HomeTableViewController.h"
#import "HomeTableViewCell.h"
#import "HomeTableModel.h"
#import "HomeHeadleView.h"
#import "HomeManager.h"
#import "HomeDetailViewController.h"
#import "AppDelegate.h"
#import "SDCycleScrollView.h"
#import "ScrollViewController.h"
#import "MBProgressHUD.h"

#import <MJRefresh.h>

@interface HomeTableViewController ()<SDCycleScrollViewDelegate>
@property (nonatomic,strong) HomeManager * homeManager;
@property (nonatomic,strong) NSMutableArray * scrollUrlArr;
@property (nonatomic,strong) MBProgressHUD * HUD;
@end

@implementation HomeTableViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollUrlArr = [NSMutableArray new];
    self.homeManager = [HomeManager homeManager];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    [self.homeManager getHomeCompletionHandle:^{
        NSLog(@"请求完成");
        [self.tableView reloadData];
        [self.HUD performSelectorOnMainThread:@selector(hide:) withObject:nil waitUntilDone:YES];
    }];
    
    // 网络监听
    AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSParameterAssert([appDlg.reach isKindOfClass: [Reachability class]]);
    NetworkStatus status = [appDlg.reach currentReachabilityStatus];
    
    if (status == NotReachable) {
        //        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"网络已断开" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        //        [alertView show];
        NSLog(@"----Notification Says Unreachable");
    }else if(status == ReachableViaWWAN){
        //        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"移动网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        //        [alertView show];
        NSLog(@"----Notification Says mobilenet");
    }else if(status == ReachableViaWiFi){
        //        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"WIfi网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        //        [alertView show];
        NSLog(@"----Notification Says wifinet");
    }

    // 下拉刷新
    [self normalHeader];
    
    // 上拉加载
    [self normalFooter];
    
    [self setupUI];
}

// 下拉刷新
- (void)normalHeader{
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        sleep(1);
        self.homeManager.UPDrow = NO;
        [self.homeManager getHomeCompletionHandle:^{
            [self.tableView reloadData];
        }];
        [self.tableView.mj_header endRefreshing];
    }];
    self.tableView.mj_header = header;
}

// 上拉加载
- (void)normalFooter{
    MJRefreshAutoFooter * footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        sleep(1);
        self.homeManager.UPDrow = YES;
        [self.homeManager getHomeCompletionHandle:^{
            [self.tableView reloadData];
        }];
    [self.tableView.mj_footer endRefreshing];
}];
    self.tableView.mj_footer = footer;
}

- (void)setupUI{
    
    HomeHeadleView *homeHeadleView = [HomeHeadleView item];
    [homeHeadleView setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 400)];
    
    // 轮播图
    [self.homeManager getScrollArrComplete:^(NSArray *picArr, NSArray *titleArr,NSMutableArray * scrollArr) {
        SDCycleScrollView *sdcScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, homeHeadleView.ScrollView.frame.size.width, homeHeadleView.ScrollView.frame.size.height) imageURLStringsGroup:picArr];
        sdcScrollView.autoScrollTimeInterval = 5;
        sdcScrollView.showPageControl = YES;
        sdcScrollView.dotColor = [UIColor whiteColor];
        sdcScrollView.pageControlDotSize = CGSizeMake(5, 5);
        sdcScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        sdcScrollView.delegate = self;
        
        sdcScrollView.titleArr = titleArr;
        self.scrollUrlArr = scrollArr;
        
        [homeHeadleView.ScrollView addSubview:sdcScrollView];
    }];
    
    
    [homeHeadleView.MissButton addTarget:self action:@selector(MissButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [homeHeadleView.XiaoCangButton addTarget:self action:@selector(XiaoCangButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [homeHeadleView.XiaoZhiButton addTarget:self action:@selector(XiaoZhiButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [homeHeadleView.MoreButton addTarget:self action:@selector(MoreButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [homeHeadleView.InquireButton addTarget:self action:@selector(InquireButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [homeHeadleView.EncycloButton addTarget:self action:@selector(EncycloButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [homeHeadleView.HeroButton addTarget:self action:@selector(HeroButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView.separatorColor = [UIColor whiteColor];
    self.tableView.tableHeaderView = homeHeadleView;
    [self.tableView registerClass:[HomeTableViewCell class] forCellReuseIdentifier:@"cell"];

}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    ScrollViewController * scrollVC = [ScrollViewController new];
    scrollVC.url = self.scrollUrlArr[index];
    [self.navigationController pushViewController:scrollVC animated:YES];
}

/*
 英雄按钮
 */
- (void)HeroButtonAction:(UIButton *)button{
    NSLog(@"213");
}

/*
 百科按钮
 */
- (void)EncycloButtonAction:(UIButton *)button{
    NSLog(@"321");
}

/*
 查询按钮
 */
- (void)InquireButtonAction:(UIButton *)button{
    NSLog(@"123");
}

/*
 Miss按钮
 */
- (void)MissButtonAction:(UIButton *)button{
    
    NSLog(@"1");
    
}

/*
 小仓按钮
 */
- (void)XiaoCangButtonAction:(UIButton *)button{

    NSLog(@"2");
    
}

/*
 若风按钮
 */
- (void)XiaoZhiButtonAction:(UIButton *)button{

    NSLog(@"3");
    
}

/*
 更多按钮
 */
- (void)MoreButtonAction:(UIButton *)button{

    NSLog(@"4");
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.homeManager getHomeResourceCount];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HomeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    HomeTableModel * homeModel = [self.homeManager getHomeModelWithIndex:indexPath.row];
    
    [cell setupViewsWithModel:homeModel];
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return @"新闻资讯";
    
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 135;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeDetailViewController * homeDetailVC = [[HomeDetailViewController alloc]init];
    HomeTableModel * homeModel = [self.homeManager getHomeModelWithIndex:indexPath.row];
    homeDetailVC.urlStr = homeModel.id;
    [self.navigationController pushViewController:homeDetailVC animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
